var wineApp = wineApp || {};
wineApp.jz = (function() {
    var params = $.url().param();
    var catId = params["cat"];
    var pageSize = 100;
    var resourceURL = wineApp.tool.getResourceURL();
    var initPage = function() {
        hightBtn();
        loadJz();
        wineApp.common.initBanner();

    }

    var loadJz = function() {
        var ajaxUrl = wineApp.CONFIG.API.jz
        wineApp.ajax.post(ajaxUrl, {
            page: 1,
            pageSize: pageSize,
            catid: catId
        }).done(onSuccess);
        //onPullUp();
    }

    var hightBtn = function() {
        $("#cat_" + catId).addClass('mui-btn-warning');
    }

    var onSuccess = function(data) {
        var template = '<dl>' +
            '<dt><a href="jz_detail.html?jzid=${id}"><img src="${img}"></a></dt>' +
            '<dd>' +
            '<a href="jz_detail.html?jzid=${id}">' +
            '<p>${catname}</p>${title}</a>' +
            '</dd>' +
            '</dl>';
        $.each(data.list, function(index, jz) {
            jz.img = resourceURL + "/" + jz.titleimg;
            var tmp = _.template(template)(jz);
            $('.wine_bnjz').append(tmp);
        });
    }

    var onPullUp = function() {
        var stopAjax = false;
        $('#scroll').infinitescroll({
            dataType: 'jsonpost',
            appendCallback: false,
            nextSelector: '#page2',
            state: {
                currPage: 1
            },
            pixelsFromNavToBottom: 80,
            data: function(currPage) {
                var request = {
                    page: currPage,
                    pageSize: pageSize
                }
                request['appid'] = wineApp.ajax.getAppid();
                request['time'] = _.now();
                var sign = wineApp.ajax.signParam(request)
                request['sign'] = sign;
                return JSON.stringify(request);

            },
            path: function(currPage) {
                var offset = currPage * pageSize;
                var ajaxUrl = wineApp.tool.getBaseApiURL() + wineApp.CONFIG.API.jz;
                return ajaxUrl;
            },
            debug: true
        }, function(data, opts) {
            if (opts.state.currPage <= data.pageCount) {
                onSuccess(data);
            } else {
                stopAjax = true;
            }
        })

    }
    return {
        initPage: initPage
    }
})();

$(document).ready(function() {
    wineApp.common.initHeaderIcon(0, false);
    wineApp.jz.initPage();
    wineApp.common.setSelectedMenu('jz');
});